package com.lz.sports.ui.manager

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.lz.comm.BR
import com.lz.comm.net.HttpManager
import com.lz.library.base.BaseActivity
import com.lz.library.extension.click
import com.lz.library.extension.resString
import com.lz.library.extension.startActivity
import com.lz.sports.R
import com.lz.sports.adapter.ExamManagerAdapter
import com.lz.sports.databinding.ActExamManagerBinding
import com.lz.sports.dialog.ExamineeImgDialog
import com.lz.sports.dialog.SelectDialog
import com.lz.helper.idcard.openHub
import com.lz.sports.core.ProjectManager
import com.lz.sports.utils.ToastUtils
import com.lz.sports.vm.manager.ExamManagerViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

//考生管理
class ExamManagerActivity : BaseActivity<ExamManagerViewModel, ActExamManagerBinding>(),
    SelectDialog.OnSelectedListener, com.lz.library.adapter.BaseAdapter.OnItemClickListener {

    companion object {
        const val REQUEST_CHECK = 1
        const val REQUEST_EXAM = 2
        const val REQUEST_UPLOAD = 3
    }

    private val adapter by lazy { ExamManagerAdapter() }

    override fun getLayoutRes(): Int = R.layout.act_exam_manager

    override fun getVMVariableId(): Int = BR.vm

    override fun initObserve() {
        viewModel.examResultList.observe(this) {
            adapter.setNewData(it)
            binding.rvList.scrollToPosition(0)
        }
        viewModel.showAll.observe(this) { adapter.changeShowState(it) }
        viewModel.operatorOver.observe(this) {
            if (it) adapter.notifyDataSetChanged()
        }
    }

    override fun initData(savedState: Bundle?) {
        binding.rvList.adapter = adapter
        adapter.onIdCardClickListener = object : ExamManagerAdapter.OnIdCardClickListener {
            override fun onIdCardClick(position: Int) {
                ExamineeImgDialog.show(this@ExamManagerActivity, adapter.getItem(position).examinee)
            }
        }
        adapter.setOnItemClickListener(this)

        lifecycleScope.launch {
            arrayOf(
                binding.ivClear,
                binding.flSearch,
                binding.tvLack,
                binding.tvAdd,
                binding.tvInvert,
                binding.tvCheck,
                binding.tvExam,
                binding.tvUploads
            ).click().collect {
                when (it.id) {
                    R.id.iv_clear -> {
                        binding.etInput.setText("")
                        viewModel.clearSearch()
                    }
                    R.id.fl_search -> viewModel.search()
                    R.id.tv_lack -> viewModel.lack(adapter.selectList())
                    R.id.tv_add -> {
                        if (ProjectManager.hasProjectInit()) {
                            if (HttpManager.hasInitServer()) {
                                openHub()
                                startActivity(ExamineeAddActivity::class.java)
                            } else {
                                ToastUtils.showMyToast(R.string.sys_server_none, Toast.LENGTH_SHORT)
                            }
                        }
                    }
                    R.id.tv_invert -> adapter.selectAll()
                    R.id.tv_check -> SelectDialog.show(
                        this@ExamManagerActivity, R.string.select.resString(),
                        viewModel.checkPosition, viewModel.checkStates, REQUEST_CHECK
                    )
                    R.id.tv_exam -> SelectDialog.show(
                        this@ExamManagerActivity, R.string.select.resString(),
                        viewModel.examPosition, viewModel.examStates, REQUEST_EXAM
                    )
                    R.id.tv_uploads -> SelectDialog.show(
                        this@ExamManagerActivity, R.string.select.resString(),
                        viewModel.uploadPosition, viewModel.uploadStates, REQUEST_UPLOAD
                    )
                }
            }
        }
    }

    override fun onSelected(dialog: SelectDialog, position: Int, requestCode: Int) {
        dialog.dismiss()
        when (requestCode) {
            REQUEST_CHECK -> viewModel.selectCheck(position)
            REQUEST_EXAM -> viewModel.selectExam(position)
            REQUEST_UPLOAD -> viewModel.selectUpload(position)
        }
    }

    override fun onItemClick(view: View, position: Int) {
        adapter.getItem(position).changeSelected()
    }
}